package com.example.seckill.controller;

import com.example.seckill.controller.vo.PromoVO;
import com.example.seckill.domain.PromoDO;
import com.example.seckill.mappstruct.PromoMap;
import com.example.seckill.service.PromoService;
import com.example.seckill.service.model.PromoModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;

@RestController
@RequestMapping("/promo")
public class PromoController {
    @Autowired
    private PromoService promoService;
    @PostMapping
    public PromoDO insert(@RequestBody PromoDO promoDO) {
        promoDO.setStartDate(LocalDateTime.now().plus(30, ChronoUnit.SECONDS).toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
        promoDO.setEndDate(LocalDateTime.now().plus(1, ChronoUnit.HOURS).toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
        promoService.save(promoDO);
        return promoDO;
    }

    @GetMapping("/{id}")
    public PromoVO getById(@PathVariable("id") Integer id) {
        PromoDO promoDO = promoService.getById(id);
        PromoModel promoModel = PromoMap.INSTANCE.convertFromDO(promoDO);
        PromoVO promoVO = PromoMap.INSTANCE.convertFromModel(promoModel);
        return promoVO;
    }
}
